const Login = require('../models').login;
const LoginAction = {};

LoginAction.getLogin = (req, res) => {
  res.jsonp({
    code: 200,
    codeMsg: "success!",
    data: {
      isLoggedIn: req.session.userId ? 1 : 0,
      userId: req.session.userId,
      session_id: req.sessionID
		}
	});
}

LoginAction.auth = (req, res, next) => {
  if ((req.session && req.session.userId) || (req.cookies && req.cookies.userId)) {
    next()
  } else if (req.query.secret === 'you__should_never_know') {
    delete req.query.secret;
    next()
  } else {
    res.json({
      error: 300,
      data: {
        desc: '您还未登陆!'
      }
    });
  }
};

LoginAction.postLogin = async (req, res) => {
  let userId = req.session.userId || req.cookies.userId;
  if(userId){
    res.status(201);
    res.jsonp({
      code: 201,
      codeMsg: "您已经登陆!"
    });
  }else{
    try {
      let { userName, password } = req.body;
      let login = await Login.findOne({
        where: {
          name: userName,
          passwd: password,
          isEnable: true
        }
      });
      if (login && login.id) {
        req.session.userId = login.id;
        req.session.nickName = login.nickName;
        res.cookie('userId', login.id, { maxAge: 60 * 1000, singed: true});
        // await req.session.save((err) => {
        //   // session saved
        // });
        res.jsonp({
          code: 200,
          codeMsg: "success!",
          data: {
            userId: login.id,
            session_id: req.sessionID
          }
        });
      } else {
        req.session.userId = 0;
        req.session.visitCnt = 0;
        req.cookies && req.cookies.userId && res.clearCookie('userId');
        res.status(301);
        res.jsonp({
          code: 301,
          codeMsg: '用户名或密码错误!'
        });
      }
    } catch (e) {
      req.session.userId = 0;
      req.session.visitCnt = 0;
      req.cookies && req.cookies.userId && res.clearCookie('userId');
      res.status(500);
      res.jsonp({
        code: 500,
        codeMsg: e
      });
    }
  }
};

LoginAction.getLoginInfo = async (req, res) => {
  try {
    let info = await Login.findOne({
      where: {
        id: userId
      }
    });
    res.jsonp({
      code: 200,
      codeMsg: "success!",
      data: info
    });
  } catch (e) {
    res.status(500);
    res.jsonp({
      code: 500,
      errorMsg: e
    });
  }
};

LoginAction.logout = (req, res) => {
  req.session.userId = 0;
  req.session.visitCnt = 0;
  req.cookies && req.cookies.userId && res.clearCookie('userId');
  res.status(202);
  res.jsonp({
    code: 202,
    codeMsg: "退出登录成功"
  });
}

LoginAction.index = (req,res) => {
  let nickName = req.session.nickName || '游客';
  if(req.session.isFirst || req.cookies.isFirst) {
    req.session.visitCnt++;
    res.jsonp({
      code: 200,
      data: {
        desc: '你好,尊敬的' + nickName + '欢迎您第'+req.session.visitCnt+'次访问!'
      }
    });
  } else {
    req.session.isFirst = 1;
    req.session.visitCnt = 1;
    res.cookie('isFirst', 1, { maxAge: 60 * 1000, singed: true});
    res.jsonp({
      code: 200,
      data: {
        desc: '你好,尊敬的' + nickName +'欢迎第一次访问。'
      }
    });
  };
};

module.exports = LoginAction;